#include "stdafx.h"
#include "DuelScreen.hpp"
#include "PositionAnimation.hpp"
#include "ColorAnimation.hpp"
#include "StateMachineState.h"
#include "StateMachine.hpp"

DuelScreen::DuelScreen(void)
{
}


DuelScreen::~DuelScreen(void)
{
}

void DuelScreen::Initialize(void)
{
	_mainTexture.loadFromFile("Resources\\duelSpritesheet.gif");
	_player1 = rh::Frame(&_mainTexture, sf::IntRect(85,56,27,25));
	_player2 = rh::Frame(&_mainTexture, sf::IntRect(327,53,31,46));

	_background = rh::Frame(&_mainTexture, sf::IntRect(2,520,256,222));
	_blackCourtain = rh::Frame(&_mainTexture, sf::IntRect(200,10,1,1));
	_blackCourtain.setScale(sf::Vector2f(256,222));	

	_player1Banner = rh::Frame(&_mainTexture, sf::IntRect(2,2,256,46));
	_player2Banner = rh::Frame(&_mainTexture, sf::IntRect(358,2,256,46));
	_player2Banner.setScale(sf::Vector2f(-1,-1));

	_neutralBanner2 = _neutralBanner1 = rh::Frame(&_mainTexture, sf::IntRect(278,520,256,46));
	_neutralBanner1.setPosition(sf::Vector2f(0,0));
	_neutralBanner1.setPosition(sf::Vector2f(0,176));

	_state = DuelState::Intro;
	InitializeState(_state);
};


void DuelScreen::Update(const sf::Time& elapsedTime, const sf::Time& totalElapsedTime)
{
	switch(_state)
	{
	case DuelState::Intro:
		(*_animations[0]).Update(elapsedTime, totalElapsedTime);
		FadeCourtainOut(elapsedTime);
		break;
	case DuelState::ShowOff:
		ShowBanners(elapsedTime);
		break;
	case DuelState::Wait:
		break;
	case DuelState::Fault:
		break;
	case DuelState::Attack:
		break;
	case DuelState::Result:
		break;
	case DuelState::Outro:
		break;
	}
};

void DuelScreen::Draw(sf::RenderWindow& renderWindow, const sf::RenderStates& renderState)
{
	_background.Draw(renderWindow, renderState);
	_player1.Draw(renderWindow, renderState);
	_player2.Draw(renderWindow, renderState);
	_blackCourtain.Draw(renderWindow, renderState);

	_neutralBanner1.Draw(renderWindow, renderState);
	_neutralBanner2.Draw(renderWindow, renderState);

	_player1Banner.Draw(renderWindow, renderState);
	_player2Banner.Draw(renderWindow, renderState);
}

void DuelScreen::FadeCourtainOut(const sf::Time& elapsedTime)
{
	if((*_animations[0]).GetAnimationState() == rh::IAnimateable::AnimationState::Completed)
	{
		delete _animations[0];
		_state = DuelState::ShowOff;
		InitializeState(_state);
	}
	else
	{
		_transitionTime -= elapsedTime;

	}
}

void DuelScreen::ShowBanners(sf::Time elapsedTime)
{
	_transitionTime  -= elapsedTime;

	switch(_introState)
	{
	case IntroState::FadeIn:
		break;
	case IntroState::Wait1:		
		if(_transitionTime <= sf::Time::Zero)
		{
			_introState = IntroState::OvershadowBg;	
			_animations[0] = new rh::ColorAnimation(&_background, sf::milliseconds(OVERSHADOW_BG), sf::Color::White, sf::Color(255,255,255,124));
			(*_animations[0]).Start();
			_transitionTime = sf::milliseconds(OVERSHADOW_BG);
		}
		break;
	case IntroState::OvershadowBg:
		(*_animations[0]).Update(elapsedTime, elapsedTime);
		if(_transitionTime <= sf::Time::Zero)
		{
			_introState = IntroState::DisplayBanners;

			_animations[0] = new rh::PositionAnimation(&_player1Banner, sf::Vector2f(-255,0), sf::Vector2f(0,0), sf::milliseconds(1000));
			_animations[1] = new rh::PositionAnimation(&_player2Banner, sf::Vector2f(256,0), sf::Vector2f(0,0), sf::milliseconds(1000));

			(*_animations[0]).Start();
			(*_animations[1]).Start();
			
			_blackCourtain.setColor(sf::Color(255,255,255,0));
			_transitionTime = sf::Time(sf::milliseconds(SLIDE_IN_BANNER_mS));
			_state = DuelState::ShowOff;
			_player1Banner.setVisibility(true);

			_neutralBanner1.setVisibility(true);
			_neutralBanner2.setVisibility(true);

			
		}
		break;
	}
}

void DuelScreen::InitializeState(DuelState state)
{
	switch(state)
	{
	case DuelState::Intro:
		_transitionTime = sf::Time(sf::milliseconds(FADE_IN_TIME_mS));
		_player1.setPosition(sf::Vector2f(30,130));
		_player2.setPosition(sf::Vector2f(180,110));
		_player1Banner.setVisibility(false);
		_player2Banner.setVisibility(false);
		_neutralBanner1.setVisibility(false);
		_neutralBanner2.setVisibility(false);

		_animations[0] = new rh::ColorAnimation(&_blackCourtain, sf::milliseconds(FADE_IN_TIME_mS), sf::Color(255,255,255,255), sf::Color(255,255,255,0));
		(*_animations[0]).Start();
		break;
	case DuelState::ShowOff:
		_introState = IntroState::Wait1;
		_transitionTime = sf::milliseconds(INTRO_WAIT_mS);
		break;
	case DuelState::Wait:
		break;
	case DuelState::Fault:
		break;
	case DuelState::Attack:
		break;
	case DuelState::Result:
		break;
	case DuelState::Outro:
		break;
	}
}

